<template>
  <div>
    <el-dialog v-model="dialogVisible" title="物料信息" :width="'70%'" class="allocation-dialog" :close-on-click-modal="false">
      <div>
        <el-tabs v-model="activeLevel" class="level-tabs">
          <el-tab-pane name="in">
            <el-table :data="totalEnergyData" border class="allocation-table">
              <el-table-column prop="name" />
              <el-table-column prop="value" />
            </el-table>
            <!-- <el-empty v-else description="暂无数据" /> -->
          </el-tab-pane>
        </el-tabs>
      </div>

      <!-- 底部按钮区域 -->
      <template #footer>
        <span class="dialog-footer">
          <template v-if="!isInputMode">
            <el-button @click="dialogVisible = false" type="primary">确认</el-button>
          </template>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from "vue";
import { getMaterialListData } from "@/utils/localStorageHandler";

const materialList = ref(getMaterialListData());
const dialogVisible = ref(false);
const isInputMode = ref(false);
const currentParams = ref();
const informType = ref();
interface InfoItem {
  name: string;
  value: any;
}

/**
 * 当前选中的层次
 */
const activeLevel = ref("");

/**
 * 找到所有未分配的物料
 */
// const currentAllocation = computed(() => {
//   return materialList.value.filter(item => item.id === currentParams.value);
// });

// 获取第一个可见的 tab
const getFirstVisibleTab = computed(() => {
  return "in"; // 默认显示tab
});

// 自制数据展示
const totalEnergyData = computed(() => {
  const totalEnergy = materialList.value.filter(item => item.id === currentParams.value);
  if (!totalEnergy) return [];
  let result: InfoItem[] = [];
  result.push({ name: "id", value: totalEnergy[0].materialId });
  result.push({ name: "产品名称", value: totalEnergy[0].materialName });
  result.push({ name: "创建时间", value: totalEnergy[0].createTime });
  return result;
});

// 打开对话框方法
const openDialog = (params1: string, params2: number) => {
  currentParams.value = params1;
  informType.value = params2;
  activeLevel.value = getFirstVisibleTab.value;
  dialogVisible.value = true;
};

// 暴露方法给父组件
defineExpose({
  openDialog
});
</script>

<style scoped>
.allocation-dialog :deep(.el-dialog) {
  position: relative !important;
  top: 10vh !important;
  display: flex;
  flex-direction: column;
  height: 600px !important;
}

/* 录入模式时调整位置 */
.allocation-dialog :deep(.el-dialog)[aria-label="分配数据管理"] {
  top: 5vh !important;
}
.allocation-dialog :deep(.el-dialog__header) {
  padding: 15px 20px;
  margin: 0;
  border-bottom: 1px solid #dcdfe6;
}
.allocation-dialog :deep(.el-dialog__footer) {
  padding: 15px 20px;
  border-top: 1px solid #dcdfe6;
}
.allocation-dialog :deep(.el-tabs) {
  display: flex;
  flex-direction: column;
}
.allocation-dialog :deep(.el-tabs__content) {
  flex: 1;
}
.allocation-dialog :deep(.el-tab-pane) {
  padding: 20px 0;
}
.allocation-table {
  width: 100%;
}
.level-tabs {
  margin-bottom: 20px;
}
.dialog-footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}
.info-bar {
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
  margin: 20px 0;
  background-color: #f5f7fa;
  border-radius: 4px;
}
.info-item {
  font-size: 14px;
}
.info-item strong {
  margin-right: 8px;
  font-weight: bold;
  color: #333333;
}

/* 总用量表格样式优化 */
.allocation-table :deep(.el-table__header) {
  background-color: #f5f7fa;
}
.allocation-table :deep(.el-table__row) {
  height: 50px;
}
</style>
